package com.novellotus.iov.carvice.model;

import android.widget.EditText;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.novellotus.iov.carvice.activity.SearchTaskActivity;
import com.novellotus.iov.carvice.adapter.TaskAdapter;
import com.novellotus.iov.carvice.bean.RxTaskListBean;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Rich on 2017/8/30.
 */

public class SearchTaskModel {

    private SearchTaskActivity context;
    private SearchInterface searchInterface;
    private final int PAGE_SIZE = 10;

    public SearchTaskModel(SearchTaskActivity context, SearchInterface searchInterface) {
        this.context = context;
        this.searchInterface = searchInterface;
    }

    /**
     * @param editText 编辑框监听
     */
    public void setSearchEditText(EditText editText) {
        RxTextView.textChanges(editText).subscribe(new Consumer<CharSequence>() {
            @Override
            public void accept(CharSequence charSequence) throws Exception {
                if (charSequence.length() > 0) {
                    searchInterface.searchTask();
                }
            }
        });
    }


    /**
     * 获取车辆列表
     */
    public void getList(EditText editText, final PageView pageView, final TaskAdapter taskAdapter) {
        Observable<BaseEntity<RxTaskListBean>> observable = RetrofitFactory.getInstance().getTaskList(SharedPreferencesUtils.getKey(context)[0], editText.getText().toString(), PAGE_SIZE, pageView.getLoadCurrentPage());
        observable.compose(RxSchedulers.<BaseEntity<RxTaskListBean>>lifecycle(context))
                .compose(RxSchedulers.<BaseEntity<RxTaskListBean>>compose())
                .subscribe(new BaseObserver<RxTaskListBean>(context) {
                    @Override
                    public void onSuccess(RxTaskListBean response) {
                        pageView.loadSuccess();
                        pageView.setTotalPage(response.getTotal(), PAGE_SIZE);

                        if (response.getCurrent() == 1) {
                            taskAdapter.update(response.getRecords());

                        } else {
                            taskAdapter.addData(response.getRecords());
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pageView.loadFail(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        pageView.loadFail("加载失败", true);
                    }
                });
    }

    public interface SearchInterface {
        void searchTask();
    }
}
